package org.truenewx.tnxjee.repo.redis.support;

import java.io.Serializable;

import org.truenewx.tnxjee.core.Strings;
import org.truenewx.tnxjee.model.entity.Entity;
import org.truenewx.tnxjee.repo.cache.EntityCacheRepo;

/**
 * ForValue方式的Redis缓存仓库支持
 *
 * @param <T> 实体类型
 * @author jianglei
 */
public abstract class RedisEntityCacheRepoSupport<T extends Entity, K extends Serializable>
        extends RedisCacheRepoSupport<T, K> implements EntityCacheRepo<T, K> {

    @Override
    protected String getKeyPrefix() {
        return getValueClass().getName() + Strings.COLON;
    }

    /**
     * 获取指定值对象的缓存关键字值
     *
     * @param object 值对象
     * @return 指定值对象的缓存关键字值
     */
    protected abstract K getKeyValue(T object);

    @Override
    public void save(T entity) {
        K keyValue = getKeyValue(entity);
        getAccessTemplate().set(keyValue, entity);
    }

    @Override
    public void delete(T entity) {
        K keyValue = getKeyValue(entity);
        delete(keyValue);
    }

}
